*----------------------------------------------------------------------------------------------------------	* 
* RESEARCHERS:		EARN												   									*
* PROGRAMMED BY:	Mette Foged and Cynthia van der Werf													*
* DESCRIPTION:		Combines admission and immigration data.					 						 	*
* CREATED:			Oct. 3, 2017																		   	*
* LAST MODIFIED:	May 31, 2022													       					*
*----------------------------------------------------------------------------------------------------------	*
	

clear 
set more off
global input "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"




*--------------------------------------------------------------------------------------------------------	


/* LOAD ADMISSION DATA AND CONVERT TO WIDE FORM */
use "$input\Admissions_latest_record", replace
drop referencetid

xtset pnr count

reshape wide forklar grundlag imputeret kategori tilladelsesdato, i(pnr) j(count)

tempfile admissions_wide

save `admissions_wide'

*--------------------------------------------------------------------------------------------------------	


/* LOAD FIRST_TIME IMMIGRATION FROM JANUARY 1st 1997 */
use if (first_immi==1 & year>1996) using "$input\Immigrations", replace

drop first_immi


*--------------------------------------------------------------------------------------------------------	


/* MERGE AND KEEP ONLY MATCHED OBSERVATIONS */
merge 1:1 pnr using `admissions_wide'
keep if _merge==3
drop _merge


*--------------------------------------------------------------------------------------------------------	


/* SELECT THE FIRST ADMISSION CATEGORY THE PERSON OBTAINS. IF IT IS PRIOR TO ENTRY SELECT THE NEXT 
ADMISSION CATEGORY IF THIS IS ALSO PRIOR TO ENTRY OR AT THE DATE OF IMMIGRATION */
gen forklar= forklar1
gen grundlag=grundlag1
gen kategori=kategori1
gen tilladelsesdato=tilladelsesdato1
gen imputeret=imputeret1

drop forklar1 grundlag1 kategori1 imputeret1 tilladelsesdato1

forvalue v=2/17 {
foreach var in forklar grundlag kategori imputeret tilladelsesdato {
	replace `var'=`var'`v' if tilladelsesdato`v' <= dato & tilladelsesdato`v'!=.
drop `var'`v'
}
}

format tilladelsesdato %td

// Imputed date of approval 
replace tilladelsesdato=. if imputeret==1 

// Difference between 
gen datdif=dato-tilladelsesdato

/*  ADD labels to the variables  */
label var forklar         "Admission category level 3"
label var grundlag        "Admission category level 2"
label var imputeret       "Imputed"
label var kategori        "Admission category level 1"
label var pnr             "Person id"
label var tilladelsesdato "Date of approval"

// Kategori
label define kat 1	"Asylum" ///
				2	"Other" ///
				3	"Work" ///
				4	"EU/EEA" ///
				5	"Family" ///
				6	"Study"
label values kategori kat				

//These variables don't have any information 
drop forklar18 grundlag18 imputeret18 kategori18 tilladelsesdato18 forklar19 grundlag19 imputeret19 kategori19 tilladelsesdato19 forklar20 grundlag20 imputeret20 kategori20 tilladelsesdato20

*--------------------------------------------------------------------------------------------------------	


*SAVE DATA
saveold "${input}\First_immigration_and_admission_latest_record.dta", version(13) replace


*ERASE TEMPORARY DATA
erase "${input}\Admissions_latest_record.dta"
erase "${input}\Immigrations.dta"
